package net.gd.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;

public class JdbcUtils {  
//	//数据库用户名  
//    private static final String USERNAME = ApplicationConf.getProperty("jdbc.connection.userName");
//    //数据库密码  
//    private static final String PASSWORD = ApplicationConf.getProperty("jdbc.connection.password");  
//    //驱动信息   
//    private static final String DRIVER = ApplicationConf.getProperty("jdbc.connection.driver"); 
//
//    //数据库地址  
//    private static final String URL = ApplicationConf.getProperty("jdbc.connection.string"); 
	//数据库用户名  
    private static final String USERNAME = ConfigurationManager.getProperty(Constants.JDBC_USER);  
    //数据库密码  
    private static final String PASSWORD = ConfigurationManager.getProperty(Constants.JDBC_PASSWORD);  
    //驱动信息   
    private static final String DRIVER = ConfigurationManager.getProperty(Constants.JDBC_DRIVER);  

    //数据库地址  
    private static final String URL = ConfigurationManager.getProperty(Constants.JDBC_URL);  

    private Connection connection = null;  
    public JdbcUtils() {  
        try{  
            Class.forName(DRIVER);  
            System.out.println("数据库连接成功！");  
        }catch(Exception e){  
        	e.printStackTrace();
        }  
    }
	private static JdbcUtils instance = null;
	
	/**
	 * 获取单例
	 * @return 单例
	 */
	public static JdbcUtils getInstance() {
		if(instance == null) {
			synchronized(JdbcUtils.class) {
				if(instance == null) {
					instance = new JdbcUtils();
				}
			}
		}
		return instance;
	}
      
    /** 
     * 获得数据库的连接 
     * @return 
     * @return 
     */  
	public synchronized Connection getConnection(){  
        try {  
        	connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);  
        } catch (SQLException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        return connection;  
    }  
	
	public static void main(String[] args) {
		JdbcUtils te = new JdbcUtils();
		te.getConnection();
	}
  
} 
